package org.jeecg.modules.wms.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.wms.entity.FinishedGoodsInbound;
import org.jeecg.modules.wms.service.IFinishedGoodsInboundService;
import org.jeecg.modules.wms.vo.CreateInboundByOrderVO;
import org.jeecg.modules.wms.vo.FinishedGoodsInboundVO;
import org.jeecg.modules.wms.vo.InboundCreateVO;
import org.jeecg.modules.wms.vo.ProductionTaskVO;
import org.jeecg.modules.wms.vo.BatchInboundExecuteVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * 成品入库管理Controller
 */
@Api(tags = "成品入库管理")
@RestController
@RequestMapping("/wms/finished-goods-inbound")
@Slf4j
public class FinishedGoodsInboundController extends JeecgController<FinishedGoodsInbound, IFinishedGoodsInboundService> {

    @Autowired
    private IFinishedGoodsInboundService finishedGoodsInboundService;

    /**
     * 分页查询入库单列表
     */
    @AutoLog(value = "成品入库-分页列表查询")
    @ApiOperation(value = "成品入库-分页列表查询", notes = "成品入库-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<FinishedGoodsInboundVO>> queryPageList(FinishedGoodsInboundVO query,
                                                               @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                               HttpServletRequest req) {
        Page<FinishedGoodsInboundVO> page = new Page<>(pageNo, pageSize);
        IPage<FinishedGoodsInboundVO> pageList = finishedGoodsInboundService.queryInboundList(page, query);
        return Result.OK(pageList);
    }

    /**
     * 创建成品入库单
     */
    @AutoLog(value = "成品入库-创建入库单")
    @ApiOperation(value = "成品入库-创建入库单", notes = "成品入库-创建入库单")
    @PostMapping(value = "/create")
    public Result<String> createInbound(@Valid @RequestBody InboundCreateVO vo) {
        try {
            String inboundId = finishedGoodsInboundService.createInbound(vo);
            return Result.OK(inboundId, "入库单创建成功");
        } catch (Exception e) {
            log.error("创建入库单失败", e);
            return Result.error("创建入库单失败：" + e.getMessage());
        }
    }

    /**
     * 执行成品入库
     */
    @AutoLog(value = "成品入库-执行入库")
    @ApiOperation(value = "成品入库-执行入库", notes = "成品入库-执行入库")
    @PostMapping(value = "/execute/{id}")
    public Result<String> executeInbound(@PathVariable String id) {
        try {
            finishedGoodsInboundService.executeInbound(id);
            return Result.OK("入库执行成功");
        } catch (Exception e) {
            log.error("执行入库失败", e);
            return Result.error("执行入库失败：" + e.getMessage());
        }
    }


    /**
     * 查询入库单详情
     */
    @AutoLog(value = "成品入库-详情查询")
    @ApiOperation(value = "成品入库-详情查询", notes = "成品入库-详情查询")
    @GetMapping(value = "/detail/{id}")
    public Result<FinishedGoodsInboundVO> queryDetail(@PathVariable String id) {
        FinishedGoodsInboundVO detail = finishedGoodsInboundService.queryInboundDetail(id);
        if (detail == null) {
            return Result.error("入库单不存在");
        }
        return Result.OK(detail);
    }

    /**
     * 删除入库单
     */
    @AutoLog(value = "成品入库-删除")
    @ApiOperation(value = "成品入库-删除", notes = "成品入库-删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        try {
            FinishedGoodsInbound inbound = finishedGoodsInboundService.getById(id);
            if (inbound == null) {
                return Result.error("入库单不存在");
            }
            if (inbound.getInboundStatus() == 2) {
                return Result.error("已完成的入库单不能删除");
            }
            finishedGoodsInboundService.removeById(id);
            return Result.OK("删除成功");
        } catch (Exception e) {
            log.error("删除入库单失败", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }

    /**
     * 批量删除入库单
     */
    @AutoLog(value = "成品入库-批量删除")
    @ApiOperation(value = "成品入库-批量删除", notes = "成品入库-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        try {
            this.service.removeByIds(java.util.Arrays.asList(ids.split(",")));
            return Result.OK("批量删除成功");
        } catch (Exception e) {
            log.error("批量删除入库单失败", e);
            return Result.error("批量删除失败：" + e.getMessage());
        }
    }

    /**
     * 根据订单编号生成成品入库单
     */
    @AutoLog(value = "成品入库-根据订单生成入库单")
    @ApiOperation(value = "成品入库-根据订单生成入库单", notes = "根据订单编号生成成品入库单")
    @PostMapping(value = "/createByOrder")
    public Result<String> createInboundByOrder(@RequestBody CreateInboundByOrderVO vo) {
        try {
            String inboundId = finishedGoodsInboundService.createInboundByOrder(vo.getOrderNo(), vo.getProductionPlanId());
            return Result.OK(inboundId, "根据订单生成入库单成功");
        } catch (Exception e) {
            log.error("根据订单生成入库单失败", e);
            return Result.error("生成入库单失败：" + e.getMessage());
        }
    }

    /**
     * 分批次执行成品入库
     */
    @AutoLog(value = "成品入库-分批次执行入库")
    @ApiOperation(value = "成品入库-分批次执行入库", notes = "分批次执行成品入库，支持多库位分配")
    @PostMapping(value = "/executeBatch")
    public Result<String> executeBatchInbound(@Valid @RequestBody BatchInboundExecuteVO vo) {
        try {
            finishedGoodsInboundService.executeBatchInbound(vo);
            return Result.OK("分批次入库执行成功");
        } catch (Exception e) {
            log.error("分批次入库执行失败", e);
            return Result.error("分批次入库执行失败：" + e.getMessage());
        }
    }
}
